package cc.facesign.acl.controller;

import cc.facesign.acl.service.TblUserService;
import cc.facesign.acl.utils.GaineUtils;
import cc.facesign.common.utils.bean.R;
import cc.facesign.security.bean.LoginUser;
import cc.facesign.security.bean.TblUser;
import cc.facesign.servicebase.utils.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/acl/account")
@Api(description = "账户相关")
public class AccountController {


    @Autowired
    private TblUserService userService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private GaineUtils gaineUtils;

    /**
     * 手机密码登录，验证失败 20001
     * 用户token存入redis
     * @return
     */
    @ApiOperation("账号登录")
    @PostMapping("/login")
    public R login(String phone, String password) {
        TblUser user = new TblUser();
        user.setPhone(phone);
        user.setPassword(password);
        return userService.login(user);
    }

    @ApiOperation("账号退出")
    @GetMapping("/logout")
    public R logout() {
        LoginUser loginUser = gaineUtils.getLoginUser();
        String userId = loginUser.getUser().getId();
        redisCache.deleteObject("login:" + userId);

        return R.ok();
    }
}
